/*
 * Copyright (C) 2006 Roman Krashanitsa
 *
 * @author Roman Krashanitsa
 *
 * This file is part of paparazzi.
 *
 * paparazzi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * paparazzi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with paparazzi; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA. 
 *
 */

package pprz.components;

import java.awt.Button;
import java.awt.Choice;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.List;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Collection;
import java.util.Enumeration;

import javax.comm.CommPortIdentifier;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.BoxLayout;

import pprz.Link;
import pprz.data_components.Dev2Proto;
import pprz.data_components.DevProtocol;
import pprz.data_components.Device;
import pprz.data_components.MsgProtocol;
import pprz.data_components.PprzMsgProtocol;
import pprz.data_components.XBee;

public class HwSelect extends Dialog implements ActionListener {

	protected static final long serialVersionUID = 1L;
	protected Choice device = null;
	protected Choice path = null;
	protected Choice protocol = null;
	protected Button add = null;
	protected Panel devConf = null;
	protected Panel buttons = null;
	protected Button done = null;
	protected List list = null;
	protected Link link;
	protected Button remove = null;
	
	/**
	 * @param owner
	 */
	public HwSelect(Link link,Frame owner) {
		super(owner);
		this.link=link;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	protected void initialize() {
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setSize(300, 294);
		this.setTitle("Device configuration");
		this.setName("hardware");
		this.add(getList(), null);
		this.add(getDevConf(), null);
		this.add(getButtons(), null);
		if (link!=null && link.getDevProtoList()!=null)
		{
			Collection<Dev2Proto> c=link.getDevProtoList();
			for (Dev2Proto p:c)
			{
				StringBuffer s=new StringBuffer();
				if (p._dev.getClass().equals(XBee.class))
					s.append("XBee");
				else {/*add here*/}
				s.append("-->").append(p._dev.getPortName());
				if (p._dec.getClass().equals(PprzMsgProtocol.class))
					s.append("XBee Message protocol");
				else {/*add here*/}
			}
		}			
	}

	/**
	 * This method initializes device	
	 * 	
	 * @return java.awt.Choice	
	 */
	protected Choice getDevice() {
		if (device == null) {
			device = new Choice();
			device.add("XBee Pro modem");
		}
		return device;
	}

	/**
	 * This method initializes path	
	 * 	
	 * @return java.awt.Choice	
	 */
	protected Choice getPath() {
		if (path == null) {
			path = new Choice();
			Enumeration e=CommPortIdentifier.getPortIdentifiers();
			CommPortIdentifier p=(CommPortIdentifier)e.nextElement();
			while(p!=null)
			{
				path.add(p.getName());
				p=(CommPortIdentifier)e.nextElement();
			}
		}
		return path;
	}

	/**
	 * This method initializes protocol	
	 * 	
	 * @return java.awt.Choice	
	 */
	protected Choice getProtocol() {
		if (protocol == null) {
			protocol = new Choice();
			protocol.add("XBee protocol");
			protocol.add("Pprz protocol");
		}
		return protocol;
	}

	/**
	 * This method initializes add	
	 * 	
	 * @return java.awt.Button	
	 */
	protected Button getAdd() {
		if (add == null) {
			add = new Button();
			add.setLabel("Add");
			add.setName("add");
			add.addActionListener(this);
		}
		return add;
	}

	/**
	 * This method initializes devConf	
	 * 	
	 * @return java.awt.Panel	
	 */
	protected Panel getDevConf() {
		if (devConf == null) {
			devConf = new Panel();
			devConf.setLayout(new BoxLayout(getDevConf(), BoxLayout.X_AXIS));
			devConf.add(getDevice(), null);
			devConf.add(getPath(), null);
			devConf.add(getProtocol(), null);
		}
		return devConf;
	}

	/**
	 * This method initializes buttons	
	 * 	
	 * @return java.awt.Panel	
	 */
	protected Panel getButtons() {
		if (buttons == null) {
			buttons = new Panel();
			buttons.setLayout(new BoxLayout(getButtons(), BoxLayout.X_AXIS));
			buttons.setMaximumSize(new Dimension(160, 23));
			buttons.setMinimumSize(new Dimension(160, 23));
			buttons.add(getDone(), null);			
			buttons.add(getAdd(), null);
			buttons.add(getRemove(), null);
		}
		return buttons;
	}

	/**
	 * This method initializes done	
	 * 	
	 * @return java.awt.Button	
	 */
	protected Button getDone() {
		if (done == null) {
			done = new Button();
			done.setLabel("Done");
			done.setName("done");
			done.addActionListener(this);
		}
		return done;
	}

	/**
	 * This method initializes textArea	
	 * 	
	 * @return java.awt.TextArea	
	 */
	protected List getList() {
		if (list == null) {
			list=new List();
			list.setMultipleMode(false);
			list.setPreferredSize(new Dimension(200, 100));
			list.setMinimumSize(new Dimension(40, 20));
		}
		return list;
	}

	public void actionPerformed(ActionEvent arg0) {
		if (link==null) return;
		if (arg0.getSource().equals(done))
		{
			this.setVisible(false);
			try	{
				link.initDevices();
			} catch (IOException e) {
			
			} catch (UnsupportedCommOperationException e) {
				
			}
		} else if (arg0.getSource().equals(remove))
		{
			int i=list.getSelectedIndex();
			list.remove(i);
			
			if (link!=null) 
			{
				if (!Link.sim_msg) link.getDevProtoList().remove(i);
			}
			
			list.repaint(100);

		}
		else if (arg0.getSource().equals(add))
		{
			String s=new StringBuffer().
				  	   append(device.getSelectedItem()).
				  	   append("-->").
				  	   append(path.getSelectedItem()).
				  	   append("-->").				  	   
				  	   append(protocol.getSelectedItem()).
				  	   toString();
			list.add(s);
			
			Device dev=null;
			MsgProtocol prot=null;
			
			switch (protocol.getSelectedIndex())
			{
			case  0:
				prot=new PprzMsgProtocol(link);
				break;
			default:
				System.out.println("protocol not found");
			}
			
			switch (device.getSelectedIndex())
			{
			case  0:
				dev=new XBee(path.getSelectedItem(),link.myAddress,"Ground Station");
				break;
			default:
				System.out.println("device not found");
			}
			
			if (dev!=null && prot!=null)
			{
				Dev2Proto dl=new Dev2Proto(dev,new DevProtocol(),prot);
				if (!Link.sim_msg) link.getDevProtoList().add(dl);
			}
		}
		this.validate();
	}

	/**
	 * This method initializes remove	
	 * 	
	 * @return java.awt.Button	
	 */
	protected Button getRemove() {
		if (remove == null) {
			remove = new Button();
			remove.setActionCommand("Remove");
			remove.setName("remove");
			remove.setLabel("Remove");
			remove.addActionListener(this);
		}
		return remove;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
